<?php

class WareClassHelper {
	
    /** 返回给定id的完整 服务地址名 */
    public function getTruePath($id, $slice='')
    {
        if(0==$id) return null;
        $adata      = WareClass::find($id);
        if($adata)
        {
            if($adata['pid']==0)    return $adata['title'];
            $title  = array();
            $road   = array_diff(explode('/', $adata['path']), array('',null,0));
            $tmp    = WareClass::getForList(array('id','IN',$road));
            foreach($road as $aid){
                $title[]	= $tmp[$aid]['title'];
            }
            $title[]	=  $adata['title'];
            return join($slice, $title);
        }
        return null;
    }

    /** 返回给定id的完整 服务地址名。 取沿途各节点 */
    public function getPathNode($id)
    {
        if(0==$id) return null;
        $adata = WareClass::find($id);
        if($adata)
        {
            if($adata['pid']==0)    return array($adata);
            $road = array_filter(explode('/', $adata['path']));
            $tmp  = WareClass::getForList(array('id','IN',$road));
            $alist = array();
            foreach($road as $aid){
                $alist[] = $tmp[$aid];
            }
            $alist[] = $adata;
            return $alist;
        }
        return null;
    }

    /**
     * 传入stuffcls_path型字串，返回path中各id的一级子元素。 取沿途各节点及其完整枝叶(一级)
     * 用途：当修改工单时，可用此方法把一系列stuffcls全部取出
     */
    public function getPathTree($path)
    {
        $ary    =   explode('/', $path);
        $ary    =   array_diff($ary, array(''));
        if(!$ary)   return null;
        $alist  =   $this->findAll('deleted=0 AND pid IN (0,'.join(',',$ary).')', 'id,pid,title', 'pid ASC,title ASC');
        if(!$alist) return null;
        foreach($alist as &$ar)
        {
            if(in_array($ar['id'], $ary))
                $ar['_sel']   =   true;
			unset($ar);
        }
        $result =   array();
        foreach($alist as $stuffcls)
        {
            $result[$stuffcls['pid']][$stuffcls['id']]    =   $stuffcls;
        }
        return $result;
    }
    /**
     * 通过最后一节点ID来取pathtree
     */
    public function getPathTreeById($id)
    {
        $id    =   intval($id);
        $adata      =   $this->find($id,'id,path');
        if(!$adata) return null;
        return $this->getPathTree($adata['path'].$adata['id']);
    }

}

